

class SubCategoryController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        params.sort = params.sort ? params.sort : "sequence" 
    	params.order = params.order ? params.order : "ASC"
        [ subCategoryInstanceList: SubCategory.list( params ), subCategoryInstanceTotal: SubCategory.count() ]
    }

    def show = {
        def subCategoryInstance = SubCategory.get( params.id )

        if(!subCategoryInstance) {
            flash.message = "SubCategory not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ subCategoryInstance : subCategoryInstance ] }
    }

    def delete = {
        def subCategoryInstance = SubCategory.get( params.id )
        if(subCategoryInstance) {
            try {
                subCategoryInstance.delete()
                flash.message = "SubCategory ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "SubCategory ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "SubCategory not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def subCategoryInstance = SubCategory.get( params.id )

        if(!subCategoryInstance) {
            flash.message = "SubCategory not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ subCategoryInstance : subCategoryInstance ]
        }
    }

    def update = {
        def subCategoryInstance = SubCategory.get( params.id )
        if(subCategoryInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(subCategoryInstance.version > version) {
                    
                    subCategoryInstance.errors.rejectValue("version", "subCategory.optimistic.locking.failure", "Another user has updated this SubCategory while you were editing.")
                    render(view:'edit',model:[subCategoryInstance:subCategoryInstance])
                    return
                }
            }
            subCategoryInstance.properties = params
            if(!subCategoryInstance.hasErrors() && subCategoryInstance.save()) {
                flash.message = "SubCategory ${params.id} updated"
                redirect(action:show,id:subCategoryInstance.id)
            }
            else {
                render(view:'edit',model:[subCategoryInstance:subCategoryInstance])
            }
        }
        else {
            flash.message = "SubCategory not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def subCategoryInstance = new SubCategory()
        subCategoryInstance.properties = params
        return ['subCategoryInstance':subCategoryInstance]
    }

    def save = {
        def subCategoryInstance = new SubCategory(params)
        if(!subCategoryInstance.hasErrors() && subCategoryInstance.save()) {
            flash.message = "SubCategory ${subCategoryInstance.id} created"
            redirect(action:show,id:subCategoryInstance.id)
        }
        else {
            render(view:'create',model:[subCategoryInstance:subCategoryInstance])
        }
    }
}
